package com.xli.sso.current.controller;

import cn.hutool.core.bean.BeanUtil;
import com.xli.dto.component.TreeNode;
import com.xli.dto.component.model.TreeModel;
import com.xli.dto.result.ResultVO;
import com.xli.dto.result.status.Status;
import com.xli.sso.current.menu.entity.CurrentMenuVO;
import com.xli.sso.current.menu.entity.mapper.ICurrentMenuMapper;
import com.xli.sso.current.router.entity.CurrentRouterVO;
import com.xli.sso.current.router.entity.mapper.ICurrentRouterMapper;
import com.xli.sso.current.user.entity.CurrentUserVO;
import com.xli.sso.current.user.entity.mapper.ICurrentUserMapper;
import com.xli.sso.security.identity.LoginUser;
import com.xli.sso.security.util.UserUtil;
import com.xli.ui.module.framemodule.entity.FrameModule;
import com.xli.ui.module.framemodule.service.IFrameModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/current")
public class CurrentController {

    @Autowired
    IFrameModuleService iFrameModuleService;

    @PostMapping(value = "/user")
    public ResultVO<CurrentUserVO> user() {
        LoginUser loginUser = UserUtil.getCurrentUser();
        if (loginUser != null) {
            CurrentUserVO currentUserVO = ICurrentUserMapper.INSTANCE.toVO(loginUser);
            List<FrameModule> frameModuleList = iFrameModuleService.findModuleListByUserId(loginUser.getUserId());
            List<String> permissionList = frameModuleList.stream()
                    .map(FrameModule::getPermission)
                    .filter(permission -> permission != null && !permission.isEmpty())
                    .distinct()
                    .toList();

            currentUserVO.setPermissions(permissionList);
            return new ResultVO<>(Status.SUCCESS, "查询成功", currentUserVO);
        }
        return new ResultVO<>(Status.FAILED, "查询失败");
    }

    @PostMapping(value = "/menu")
    public ResultVO<TreeModel<CurrentMenuVO>> menu() {
        LoginUser loginUser = UserUtil.getCurrentUser();
        if (loginUser != null) {
            TreeModel<CurrentMenuVO> treeModel = new TreeModel<>() {

                @Override
                public List<TreeNode<CurrentMenuVO>> fetch() {
                    List<FrameModule> frameModuleList = iFrameModuleService.findModuleListByUserId(loginUser.getUserId());
                    return frameModuleList.stream().filter(frameModule -> "0".equals(frameModule.getType()) || "1".equals(frameModule.getType()))
                            .map(frameModule -> {
                                TreeNode<CurrentMenuVO> treeNode = new TreeNode<>();
                                treeNode.setId(frameModule.getId());
                                treeNode.setPid(frameModule.getPid());
                                treeNode.setText(frameModule.getName());
                                treeNode.setVo(ICurrentMenuMapper.INSTANCE.toVO(frameModule));
                                return treeNode;
                            }).collect(Collectors.toList());
                }
            };
            return new ResultVO<>(Status.SUCCESS, "查询成功", treeModel);
        }
        return new ResultVO<>(Status.FAILED, "查询失败");
    }
}
